myApp.controller('containerController', function ($scope, $timeout) {

    $scope.safeApply = function (fn) {
        var phase = this.$root.$$phase;
        if (phase == "$apply" || phase == '$digest') {
            if (fn && (typeof(fn) === 'function')) {
                fn();
            }
        } else {
            this.$apply(fn);
        }
    };

    $scope.baseScore = 600;

    $scope.showList = [false, false, false, false, false, false, false, false];

    $scope.q1Statues = [false, false, true];
    $scope.q2Statues = [false, false, true];
    $scope.q3Statues = [false, false, true];
    $scope.q4Statues = [false, false, true];
    $scope.q5Statues = [false, false, true];
	$scope.q6Statues = [false, false, true];
	
    $scope.clicks = [];
    $scope.q5Selected = [false, false, false, false];


    $scope.show = function (index) {
        for (var a = 0; a < $scope.showList.length; a++) {
            $scope.showList[a] = false;
        }
        if (index > 0) {
            $scope.showList[index - 1] = true;
        }
    }

    $scope.show(1);
    //radio选中
    $scope.radioChack = function ($event) {
        $($event.target).parent().addClass('on').siblings().removeClass('on');
    }


    window.game.init();
    window.game2.init();

    //处理名字，段位计算初始得分
    $scope.showSecond = function () {
        if ($scope.name && $scope.rank) {
            //处理得分
            if ($scope.rank == '青铜' || $scope.rank == '白银') {
                $scope.baseScore = $scope.baseScore + 0;
            } else if ($scope.rank == '黄金' || $scope.rank == '铂金' || $scope.rank == '钻石') {
                $scope.baseScore = $scope.baseScore + 10;
            } else {
                $scope.baseScore = $scope.baseScore + 20;
            }
            console.log($scope.baseScore)
            $scope.show(2);
        }
    }

    $scope.showThird = function (statue) {
        if (statue == -1) {
            $scope.baseScore = $scope.baseScore + 0;
            $scope.q1Statues = [true, false, false]
        } else {
            $scope.baseScore = $scope.baseScore + 15;
            $scope.q1Statues = [false, true, false]
        }
        //设置个定时，页面跳转
        console.log($scope.baseScore)
        $timeout(function () {
            $scope.show(3);
        }, 1000);
    }

    $scope.showForth = function (statue) {
        if (statue == -1) {
            $scope.baseScore = $scope.baseScore + 0;
            $scope.q2Statues = [true, false, false]
        } else {
            $scope.baseScore = $scope.baseScore + 15;
            $scope.q2Statues = [false, true, false]
        }
        //设置个定时，页面跳转
        console.log($scope.baseScore)
        $timeout(function () {
            console.log()
            $scope.show(4);
        }, 1000);
    }

    window.__defineSetter__('q1Statue', function (val) {
        this.val = val;
        console.log("q1Statue")
        if (val != 0) {
            $scope.$apply(function () {
                $scope.showThird(val);
            })

        }
    })

    window.__defineSetter__('q2Statue', function (val) {
        this.val = val;
        console.log("q2Statue")
        if (val != 0) {
            $scope.$apply(function () {
                console.log("----")
                $scope.showForth(val)
            })
        }
    })

    $scope.addClick = function (click) {
        if ($scope.clicks.length < 5) {
            $scope.clicks.push(click);
        }
    }

    $scope.$watch("clicks.length", function () {
        if ($scope.clicks && $scope.clicks.length == 5) {

            if ($scope.clicks[0] == 'Q'
                && $scope.clicks[1] == 'W'
                && $scope.clicks[2] == 'Q'
                && $scope.clicks[3] == 'E'
                && $scope.clicks[4] == 'Q') {
                $scope.baseScore = $scope.baseScore + 15;
                $scope.q3Statues = [false, true, false]
            } else {
                $scope.baseScore = $scope.baseScore + 0;
                $scope.q3Statues = [true, false, false]
            }
            console.log($scope.baseScore)
            $timeout(function () {
                $scope.show(5)
            }, 1000)
        }
    })

    $scope.voiceRadioCheck = function (index, event) {
        if (index == 2) {
            $scope.baseScore = $scope.baseScore + 15;
            $scope.q4Statues = [false, true, false]
        } else {
            $scope.baseScore = $scope.baseScore + 0;
            $scope.q4Statues = [true, false, false]
        }
        console.log($scope.baseScore)

        $(event.target).parent().addClass('on').siblings().removeClass('on');


        $timeout(function () {
            $scope.show(6)
        }, 1000)
    }


    $scope.itemRadioCheck = function (index, event) {
        if (index != 0 && index != 2) {
            $scope.baseScore = $scope.baseScore + 0;
            $scope.q5Statues = [true, false, false]
            $timeout(function () {
                $scope.show(7)
            }, 1000)
        } else {
            $scope.q5Selected[index] = !$scope.q5Selected[index];
            if ($scope.q5Selected[0] == true && $scope.q5Selected[2] == true) {
                $scope.baseScore = $scope.baseScore + 15;
                $scope.q5Statues = [false, true, false]
                $timeout(function () {
                    $scope.show(7)
                }, 1000)
            }
        }
        
        $(event.target).parent().addClass('on');
    }

    $scope.showQ6Video = function () {
        $scope.q6Error = true;
        document.getElementById("q6Video").play();
    }

    $scope.closeQ6Video = function () {
        $scope.q6Error = false;
        document.getElementById("q6Video").pause();
    }

    $scope.zyCheck = function (index, event) {
        $(event.target).parent().addClass('on').siblings().removeClass('on');
        if (index != 1) {
            $scope.baseScore = $scope.baseScore + 0;
            $scope.q6Statues = [true, false, false]
            console.log($scope.baseScore)
            $scope.showQ6Video();
        } else {
            $scope.baseScore = $scope.baseScore + 15;
            $scope.q6Statues = [false, true, false]
            console.log($scope.baseScore)
            document.getElementById("q6Video").pause();
            $scope.show(8)
        }
    }

    $scope.toResult = function () {
        console.log($scope.baseScore)
        $scope.closeQ6Video();
        $scope.show(8)
    }


})